import { createStore } from 'vuex'

const store = createStore({
  state: {
    user: null,
    token: localStorage.getItem('token') || '',
  },
  mutations: {
    setUser (state, user) {
      state.user = user
    },
    setToken (state, token) {
      state.token = token
      localStorage.setItem('token', token)
    },
  },
  actions: {
    login ({ commit }, user) {
      // 登录逻辑
      commit('setUser', user)
      commit('setToken', user.token)
    },
    logout ({ commit }) {
      commit('setUser', null)
      commit('setToken', '')
      localStorage.removeItem('token')
    },
  },
})

export default store
